package com.ufm.security.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.ufm.security.pojo.Rights;
import com.ufm.security.pojo.Roles;
import com.ufm.security.pojo.TablesAndRoles;
import com.ufm.security.service.IRightsService;
import com.ufm.security.service.IRolesService;
import com.ufm.syslog.pojo.Syslog;
import com.ufm.syslog.service.ISyslogService;

import com.ufm.user.pojo.User;
import com.ufm.user.service.IUserService;
import com.ufm.util.TreeUtil;

@Controller
@RequestMapping("/rights")
public class RightsController {
	
	@Autowired
	private ISyslogService syslogService;
	
	@Autowired
	private IRightsService iRightsService;
	@Autowired
	private IUserService userService;

	@Autowired
	private IRolesService iRolesService;
	
	@RequestMapping(value="/goToRightsTree")
	public @ResponseBody List<TreeUtil> gotoMainPageForRights(Model model,HttpServletRequest request) {
		List<TreeUtil>  res = getListForTree(request);
		return res;
	}
	
	private List<TreeUtil> getListForTree(HttpServletRequest request){
		
		String roleId = request.getParameter("roleId");
		List<TreeUtil> datas = new ArrayList<TreeUtil>();
		List<Rights> rightsList = new ArrayList<Rights>();
		List<Rights> rightsListFromRole = new ArrayList<Rights>();
		rightsList = 	iRightsService.findAll(new HashMap());
		if(roleId!=null && !"".equals(roleId)){
			rightsListFromRole = iRightsService.findByRoleId(Integer.parseInt(roleId));
		}
		
		for (Iterator iterator4 = rightsList.iterator(); iterator4.hasNext();) {
			Rights rights = (Rights) iterator4.next();
			TreeUtil t1 = new TreeUtil(rights.getId()+"",rights.getParent_id()+"",rights.getRight_name(),true);
			if(rightsListFromRole!=null && rightsListFromRole.size()>0){
				for(int i = 0;i<rightsListFromRole.size();i++){
					Rights temp=(Rights)rightsListFromRole.get(i);
					if(temp.getId()==rights.getId()){
						t1.setChecked(true);
						break;
					}
				}
			}
			datas.add(t1);
		}
		return datas;
	}
	
	
	@RequestMapping(value="/ajaxSaveRightsAndRole")
	public @ResponseBody boolean saveRightsAndRole(Model model,HttpServletRequest request) {
		boolean flag = true;
		try{
			String vRightsId = request.getParameter("vRightsId");
			int roleId = Integer.parseInt(request.getParameter("roleId"));
			iRightsService.updateRoleAndRights(vRightsId, roleId);
			addLog(request, "角色赋权", "维护角色与权限关系");
		}catch(Exception e){
			flag = false;
		}
		return flag;	
	}

	
	@RequestMapping(value="/ajaxSaveUsersAndRole")
	public @ResponseBody boolean ajaxSaveUsersAndRole(Model model,HttpServletRequest request) {
		boolean flag = true;
		try{
			String vUsersId = request.getParameter("vUsersId");
			int roleId = Integer.parseInt(request.getParameter("roleId"));
			iRightsService.updateRoleAndUsers(vUsersId, roleId);
			addLog(request, "人员赋权", "维护角色与人员关系");
		}catch(Exception e){
			e.printStackTrace();
			flag = false;
		}
		return flag;	
	}
	
	public void addLog(HttpServletRequest request, String moduleName, String action){
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User userSession = userService.findByName(userDetails.getUsername());
		Syslog syslog = new Syslog(moduleName, action, 1, userSession.getUserId(), userSession.getUserName(), request.getRemoteAddr(), new Date());
		syslogService.insert(syslog);
	}
	
	
}